/*
 * @(#)WorkflowStepFormEquat.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowStepFormEquat;

/**
 * WorkflowStepFormEquatDAObject.java
 *
 * This class is the data access bean for table "WORKFLOW_STEP_FORM_EQUAT".
 *
 * @author Tony Chen
 * @company DCIVision Limited
 * @creation date 06/09/2004
 * @version $Revision: 1.8 $
 */

public class WorkflowStepFormEquatDAObject extends AbstractDAObject {
  
  public static final String REVISION = "$Revision: 1.8 $";
  
  public static final String TABLE_NAME = "WORKFLOW_STEP_FORM_EQUAT";
  
  public WorkflowStepFormEquatDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }
  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("WORKFLOW_STEP_ID");
    this.vecDBColumn.add("ROUTE_SEQ");
    this.vecDBColumn.add("ROUTE_NAME");
    this.vecDBColumn.add("DEFAULT_TYPE");
    this.vecDBColumn.add("OPERAND1");
    this.vecDBColumn.add("OPERAND2");
    this.vecDBColumn.add("COMPARE");
    this.vecDBColumn.add("FORM_EQUATION");
    this.vecDBColumn.add("OPERATION_TYPE");
    this.vecDBColumn.add("DATE_FROMAT");        
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }
  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ROUTE_SEQ, A.ROUTE_NAME, A.DEFAULT_FLAG, A.OPERAND1, A.OPERAND2, A.COMPARE, A.FORM_EQUATION, A.OPERATION_TYPE, A.DATE_FORMAT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_STEP_FORM_EQUAT A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          WorkflowStepFormEquat tmpWorkflowStepFormEquat = new WorkflowStepFormEquat();
          tmpWorkflowStepFormEquat.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowStepFormEquat.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpWorkflowStepFormEquat.setRouteSeq(getResultSetInteger(rs, "ROUTE_SEQ"));
          tmpWorkflowStepFormEquat.setRouteName(getResultSetString(rs, "ROUTE_NAME"));
          tmpWorkflowStepFormEquat.setDefaultFlag(getResultSetString(rs, "DEFAULT_FLAG"));
          tmpWorkflowStepFormEquat.setOperand1(getResultSetString(rs, "OPERAND1"));
          tmpWorkflowStepFormEquat.setOperand2(getResultSetString(rs, "OPERAND2"));
          tmpWorkflowStepFormEquat.setCompare(getResultSetString(rs, "COMPARE"));
          tmpWorkflowStepFormEquat.setFormEquation(getResultSetString(rs, "FORM_EQUATION"));
          tmpWorkflowStepFormEquat.setOperationType(getResultSetString(rs, "OPERATION_TYPE"));
          tmpWorkflowStepFormEquat.setDateFormat(getResultSetString(rs, "DATE_FORMAT"));
          tmpWorkflowStepFormEquat.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowStepFormEquat.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowStepFormEquat.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowStepFormEquat.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowStepFormEquat.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowStepFormEquat.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowStepFormEquat.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getCreatorID()));
          tmpWorkflowStepFormEquat.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getUpdaterID()));
          return(tmpWorkflowStepFormEquat);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ROUTE_SEQ, A.ROUTE_NAME, A.DEFAULT_FLAG, A.OPERAND1, A.OPERAND2, A.COMPARE, A.FORM_EQUATION, A.DATE_FORMAT, A.OPERATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_STEP_FORM_EQUAT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        
        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        
        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());
        
        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }
        
        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
        
        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();
        
        this.positionCursor(rs, startOffset, pageSize);
        
        while (rs.next() && rowLoopCnt<pageSize) {
          WorkflowStepFormEquat tmpWorkflowStepFormEquat = new WorkflowStepFormEquat();
          tmpWorkflowStepFormEquat.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowStepFormEquat.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpWorkflowStepFormEquat.setRouteSeq(getResultSetInteger(rs, "ROUTE_SEQ"));
          tmpWorkflowStepFormEquat.setRouteName(getResultSetString(rs, "ROUTE_NAME"));
          tmpWorkflowStepFormEquat.setDefaultFlag(getResultSetString(rs, "DEFAULT_FLAG"));
          tmpWorkflowStepFormEquat.setOperand1(getResultSetString(rs, "OPERAND1"));
          tmpWorkflowStepFormEquat.setOperand2(getResultSetString(rs, "OPERAND2"));
          tmpWorkflowStepFormEquat.setCompare(getResultSetString(rs, "COMPARE"));
          tmpWorkflowStepFormEquat.setFormEquation(getResultSetString(rs, "FORM_EQUATION"));
          tmpWorkflowStepFormEquat.setOperationType(getResultSetString(rs, "OPERATION_TYPE"));
          tmpWorkflowStepFormEquat.setDateFormat(getResultSetString(rs, "DATE_FORMAT"));
          tmpWorkflowStepFormEquat.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowStepFormEquat.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowStepFormEquat.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowStepFormEquat.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowStepFormEquat.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowStepFormEquat.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowStepFormEquat.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getCreatorID()));
          tmpWorkflowStepFormEquat.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getUpdaterID()));
          tmpWorkflowStepFormEquat.setRecordCount(totalNumOfRecord);
          tmpWorkflowStepFormEquat.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpWorkflowStepFormEquat);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }
  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ROUTE_SEQ, A.ROUTE_NAME, A.DEFAULT_FLAG, A.OPERAND1, A.OPERAND2, A.COMPARE, A.FORM_EQUATION, A.OPERATION_TYPE, A.DATE_FORMAT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_STEP_FORM_EQUAT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          WorkflowStepFormEquat tmpWorkflowStepFormEquat = new WorkflowStepFormEquat();
          tmpWorkflowStepFormEquat.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowStepFormEquat.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpWorkflowStepFormEquat.setRouteSeq(getResultSetInteger(rs, "ROUTE_SEQ"));
          tmpWorkflowStepFormEquat.setRouteName(getResultSetString(rs, "ROUTE_NAME"));
          tmpWorkflowStepFormEquat.setDefaultFlag(getResultSetString(rs, "DEFAULT_FLAG"));
          tmpWorkflowStepFormEquat.setOperand1(getResultSetString(rs, "OPERAND1"));
          tmpWorkflowStepFormEquat.setOperand2(getResultSetString(rs, "OPERAND2"));
          tmpWorkflowStepFormEquat.setCompare(getResultSetString(rs, "COMPARE"));
          tmpWorkflowStepFormEquat.setFormEquation(getResultSetString(rs, "FORM_EQUATION"));
          tmpWorkflowStepFormEquat.setOperationType(getResultSetString(rs, "OPERATION_TYPE"));
          tmpWorkflowStepFormEquat.setDateFormat(getResultSetString(rs, "DATE_FORMAT"));
          tmpWorkflowStepFormEquat.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowStepFormEquat.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowStepFormEquat.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowStepFormEquat.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowStepFormEquat.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowStepFormEquat.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowStepFormEquat.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getCreatorID()));
          tmpWorkflowStepFormEquat.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getUpdaterID()));
          result.add(tmpWorkflowStepFormEquat);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }
  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }
  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }
  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    WorkflowStepFormEquat tmpWorkflowStepFormEquat = (WorkflowStepFormEquat)((WorkflowStepFormEquat)obj).clone();
    
    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   WORKFLOW_STEP_FORM_EQUAT(ID, WORKFLOW_STEP_ID, ROUTE_SEQ, ROUTE_NAME, DEFAULT_FLAG, OPERAND1, OPERAND2, COMPARE, FORM_EQUATION, OPERATION_TYPE, DATE_FORMAT, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpWorkflowStepFormEquat.getWorkflowStepID());
        setPrepareStatement(preStat, 3, tmpWorkflowStepFormEquat.getRouteSeq());
        setPrepareStatement(preStat, 4, tmpWorkflowStepFormEquat.getRouteName());
        setPrepareStatement(preStat, 5, tmpWorkflowStepFormEquat.getDefaultFlag());
        setPrepareStatement(preStat, 6, tmpWorkflowStepFormEquat.getOperand1());
        setPrepareStatement(preStat, 7, tmpWorkflowStepFormEquat.getOperand2());
        setPrepareStatement(preStat, 8, tmpWorkflowStepFormEquat.getCompare());
        setPrepareStatement(preStat, 9, tmpWorkflowStepFormEquat.getFormEquation());
        setPrepareStatement(preStat, 10, tmpWorkflowStepFormEquat.getOperationType());
        setPrepareStatement(preStat, 11, tmpWorkflowStepFormEquat.getDateFormat());
        setPrepareStatement(preStat, 12, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 13, new Integer(0));
        setPrepareStatement(preStat, 14, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 15, currTime);
        setPrepareStatement(preStat, 16, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 17, currTime);
        preStat.executeUpdate();
        tmpWorkflowStepFormEquat.setID(nextID);
        tmpWorkflowStepFormEquat.setCreatorID(sessionContainer.getUserRecordID());
        tmpWorkflowStepFormEquat.setCreateDate(currTime);
        tmpWorkflowStepFormEquat.setUpdaterID(sessionContainer.getUserRecordID());
        tmpWorkflowStepFormEquat.setUpdateDate(currTime);
        tmpWorkflowStepFormEquat.setUpdateCount(new Integer(0));
        tmpWorkflowStepFormEquat.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getCreatorID()));
        tmpWorkflowStepFormEquat.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getUpdaterID()));
        return(tmpWorkflowStepFormEquat);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    WorkflowStepFormEquat tmpWorkflowStepFormEquat = (WorkflowStepFormEquat)((WorkflowStepFormEquat)obj).clone();
    
    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE WORKFLOW_STEP_FORM_EQUAT ");
        sqlStat.append("SET  WORKFLOW_STEP_ID=?, ROUTE_SEQ=?, ROUTE_NAME=?, DEFAULT_FLAG=?, OPERAND1=?, OPERAND2=?, COMPARE=?, FORM_EQUATION=?, OPERATION_TYPE=?, DATE_FORMAT=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpWorkflowStepFormEquat.getWorkflowStepID());
        setPrepareStatement(preStat, 2, tmpWorkflowStepFormEquat.getRouteSeq());
        setPrepareStatement(preStat, 3, tmpWorkflowStepFormEquat.getRouteName());
        setPrepareStatement(preStat, 4, tmpWorkflowStepFormEquat.getDefaultFlag());
        setPrepareStatement(preStat, 5, tmpWorkflowStepFormEquat.getOperand1());
        setPrepareStatement(preStat, 6, tmpWorkflowStepFormEquat.getOperand2());
        setPrepareStatement(preStat, 7, tmpWorkflowStepFormEquat.getCompare());
        setPrepareStatement(preStat, 8, tmpWorkflowStepFormEquat.getFormEquation());
        setPrepareStatement(preStat, 9, tmpWorkflowStepFormEquat.getOperationType());
        setPrepareStatement(preStat, 10, tmpWorkflowStepFormEquat.getDateFormat());
        setPrepareStatement(preStat, 11, new Integer(tmpWorkflowStepFormEquat.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 13, currTime);
        setPrepareStatement(preStat, 14, tmpWorkflowStepFormEquat.getID());
        setPrepareStatement(preStat, 15, tmpWorkflowStepFormEquat.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpWorkflowStepFormEquat.setUpdaterID(sessionContainer.getUserRecordID());
          tmpWorkflowStepFormEquat.setUpdateDate(currTime);
          tmpWorkflowStepFormEquat.setUpdateCount(new Integer(tmpWorkflowStepFormEquat.getUpdateCount().intValue() + 1));
          tmpWorkflowStepFormEquat.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getCreatorID()));
          tmpWorkflowStepFormEquat.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getUpdaterID()));
          return(tmpWorkflowStepFormEquat);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    WorkflowStepFormEquat tmpWorkflowStepFormEquat = (WorkflowStepFormEquat)((WorkflowStepFormEquat)obj).clone();
    
    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   WORKFLOW_STEP_FORM_EQUAT ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpWorkflowStepFormEquat.getID());
        setPrepareStatement(preStat, 2, tmpWorkflowStepFormEquat.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpWorkflowStepFormEquat);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    WorkflowStepFormEquat tmpWorkflowStepFormEquat = (WorkflowStepFormEquat)this.oldValue;
    
    if (tmpWorkflowStepFormEquat != null) {
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getWorkflowStepID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getRouteSeq()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getRouteName()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getDefaultFlag()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getOperand1()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getOperand2()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getCompare()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getFormEquation()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getOperationType()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getDateFormat()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getUpdateDate()));
    }
    
    tmpWorkflowStepFormEquat = (WorkflowStepFormEquat)obj;
    if (tmpWorkflowStepFormEquat != null) {
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getID()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getWorkflowStepID()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getRouteSeq()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getRouteName()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getDefaultFlag()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getOperand1()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getOperand2()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getCompare()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getFormEquation()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getOperationType()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getDateFormat()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpWorkflowStepFormEquat.getUpdateDate()));
    }
    
    auditTrailBase(opMode, oldValues, newValues);
  }
  
  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  /**
   * Gets list of form value driven task equation of the given step
   * 
   * @param workflowStepID The workflow step ID
   * @return List of WorkflowStepFormEquat objects
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStepFormEquat
   */
  public synchronized List getListByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ROUTE_SEQ, A.ROUTE_NAME, A.DEFAULT_FLAG, A.OPERAND1, A.OPERAND2, A.COMPARE, A.FORM_EQUATION, A.OPERATION_TYPE, A.DATE_FORMAT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_STEP_FORM_EQUAT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          WorkflowStepFormEquat tmpWorkflowStepFormEquat = new WorkflowStepFormEquat();
          tmpWorkflowStepFormEquat.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowStepFormEquat.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpWorkflowStepFormEquat.setRouteSeq(getResultSetInteger(rs, "ROUTE_SEQ"));
          tmpWorkflowStepFormEquat.setRouteName(getResultSetString(rs, "ROUTE_NAME"));
          tmpWorkflowStepFormEquat.setDefaultFlag(getResultSetString(rs, "DEFAULT_FLAG"));
          tmpWorkflowStepFormEquat.setOperand1(getResultSetString(rs, "OPERAND1"));
          tmpWorkflowStepFormEquat.setOperand2(getResultSetString(rs, "OPERAND2"));
          tmpWorkflowStepFormEquat.setCompare(getResultSetString(rs, "COMPARE"));
          tmpWorkflowStepFormEquat.setFormEquation(getResultSetString(rs, "FORM_EQUATION"));
          tmpWorkflowStepFormEquat.setOperationType(getResultSetString(rs, "OPERATION_TYPE"));
          tmpWorkflowStepFormEquat.setDateFormat(getResultSetString(rs, "DATE_FORMAT"));
          tmpWorkflowStepFormEquat.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowStepFormEquat.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowStepFormEquat.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowStepFormEquat.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowStepFormEquat.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowStepFormEquat.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowStepFormEquat.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getCreatorID()));
          tmpWorkflowStepFormEquat.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowStepFormEquat.getUpdaterID()));
          result.add(tmpWorkflowStepFormEquat);
        } 
        return result;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a number of next step seq number for given workflow step
   * 
   * @param workflowStepID The workflow step ID
   * @return Integer of The next step seq number
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStepFormEquat
   */
  public synchronized Integer getNextSeqNumber(Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Integer result = null;
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT MAX(A.ROUTE_SEQ) + 1 AS NEXT_ROUTE_SEQ ");
        sqlStat.append("FROM   WORKFLOW_STEP_FORM_EQUAT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        rs = preStat.executeQuery();
        if  (rs.next()) {
          result = this.getResultSetInteger(rs, "NEXT_ROUTE_SEQ");
          if(Utility.isEmpty(result)){
            return(new Integer(1));
          } else {
            return(result);
          }          
        } else {
          return(new Integer(1));
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Delete all object(WorkflowStepFormEquat) for given workflow record
   * 
   * @param workflowRecordID The workflow record ID
   * @throws ApplicationException
   */
  public void deleteByWorkflowStepID(Integer workflowRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   WORKFLOW_STEP_FORM_EQUAT ");
        sqlStat.append("WHERE  WORKFLOW_STEP_ID= ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, workflowRecordID);
        updateCnt = preStat.executeUpdate();
        if (updateCnt < 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  
}
